/*
 * main.cpp
 *
 *  Created on: Jun 4, 2013
 *      Author: Hernan
 */
#include "sdl/sdl.h"
#include "sdl/sdl_image.h"
#include "SDL/SDL_ttf.h"
#include "sdl/sdl_mixer.h"
#include <iostream>
#include <sstream>
#include <string>
#include <sstream>
#include <math.h>
#include <windows.h>
#include "BarraStatus.h"
using namespace std;


BarraStatus::BarraStatus(int vida, int vidaTotal,int magia,int magiatotal, string nombre){
	this->vida = vida;
	this->vidaTotal = vidaTotal;
	this->magia= magia;
	this->magiaTotal = magiatotal;
	this->nombre = nombre;
}

SDL_Surface* BarraStatus::getStatus()
{


    TTF_Init();
    TTF_Font *font = TTF_OpenFont("C:/WINDOWS/Fonts/arialbd.ttf", 12);
    TTF_Font *fuente2 = TTF_OpenFont("C:/WINDOWS/Fonts/arial.ttf", 9);
    SDL_Surface *textovida,*textomagia, *nombre;
    SDL_Surface* imagen= SDL_CreateRGBSurface(SDL_SWSURFACE, 100, 100, 32, 0, 0, 0, 0);

    Uint32 color2 = SDL_MapRGB(imagen->format, 0x67, 0x6b, 0x58);

    SDL_Color color1 = {0xff, 0xff, 0xff};

    stringstream ss;
    SDL_Rect box;

    SDL_Rect bgbox;
    SDL_Rect valBox;
    SDL_Rect valBox2;
    valBox.x = 20;
    valBox.y = 11;
    valBox2.x = 20;
    valBox2.y = 21;


    SDL_Rect nomBox;
    nomBox.x = 25;
    nomBox.y = 0;

    ss.str(" ");
    ss << this->vida;
    ss << "/";
    ss << this->vidaTotal;
    textovida = TTF_RenderText_Solid(fuente2, ss.str().c_str(), color1);

    ss.str(" ");
    ss << this->magia;
    ss << "/";
    ss << this->magiaTotal;
    textomagia = TTF_RenderText_Solid(fuente2, ss.str().c_str(), color1);

    nombre = TTF_RenderText_Solid(font, this->nombre.c_str(), color1);

    DrawBar(imagen,"vida", vida, vidaTotal, 10, 10, 50, 10, &box, &bgbox, color2);
    DrawBar(imagen,"magia", magia, magiaTotal, 10, 20, 50, 10, &box, &bgbox, color2);
    SDL_BlitSurface(textovida, NULL, imagen, &valBox);
    SDL_BlitSurface(textomagia, NULL, imagen, &valBox2);
    SDL_BlitSurface(nombre, NULL, imagen, &nomBox);


    Uint32 key = SDL_MapRGB(imagen->format, 0, 0, 0);
    SDL_SetColorKey( imagen, SDL_SRCCOLORKEY, key );
    SDL_FreeSurface(textovida);
    SDL_FreeSurface(textomagia);
    SDL_FreeSurface(nombre);
    TTF_CloseFont(font);
    TTF_CloseFont(fuente2);
    TTF_Quit();
    return imagen;
}


SDL_Surface* BarraStatus::getEntidadStatus()
{


    TTF_Init();
    TTF_Font *fuente2 = TTF_OpenFont("C:/WINDOWS/Fonts/arial.ttf", 9);
    //SDL_Surface *nombreEntidad;
    SDL_Surface* textovida;
    SDL_Surface* imagen= SDL_CreateRGBSurface(SDL_SWSURFACE, 100, 100, 32, 0, 0, 0, 0);

    Uint32 color2 = SDL_MapRGB(imagen->format, 0x67, 0x6b, 0x58);

    SDL_Color color1 = {0xff, 0xff, 0xff};

    stringstream ss;
    SDL_Rect box;

    SDL_Rect bgbox;
    SDL_Rect valBox;
    //SDL_Rect valBox2;
    valBox.x = 20;
    valBox.y = 11;
    //valBox2.x = 12;
    //valBox2.y = 21;


    //SDL_Rect nomBox;
    //nomBox.x = 25;
    //nomBox.y = 0;

    ss.str(" ");
    ss << this->vida;
    ss << "/";
    ss << this->vidaTotal;
    textovida = TTF_RenderText_Solid(fuente2, ss.str().c_str(), color1);

    //ss.str(" ");
    //ss << this->magia;
    //ss << "/";
    //ss << this->magiaTotal;
    //textomagia = TTF_RenderText_Solid(fuente2, ss.str().c_str(), color1);

    //nombre = TTF_RenderText_Solid(font, this->nombre.c_str(), color1);

    DrawBar(imagen,"vida", vida, vidaTotal, 10, 10, 50, 10, &box, &bgbox, color2);
    //DrawBar(imagen,"magia", magia, magiaTotal, 10, 20, 50, 10, &box, &bgbox, color2);
    SDL_BlitSurface(textovida, NULL, imagen, &valBox);
    //SDL_BlitSurface(textomagia, NULL, imagen, &valBox2);
    //SDL_BlitSurface(nombre, NULL, imagen, &nomBox);


    Uint32 key = SDL_MapRGB(imagen->format, 0, 0, 0);
    SDL_SetColorKey( imagen, SDL_SRCCOLORKEY, key );
    SDL_FreeSurface(textovida);
    //SDL_FreeSurface(textomagia);
    //SDL_FreeSurface(nombre);
    //TTF_CloseFont(font);
    TTF_CloseFont(fuente2);
    TTF_Quit();
    return imagen;
}

void BarraStatus::putpixel(SDL_Surface* screen, string tipo, int x, int y)
{
    Uint32 *pixel = (Uint32*)screen->pixels;
    Uint32* p = pixel + y*screen->pitch/4 + x;

    if (tipo.compare("vida")==0){
    	if (vida>50)
    		*p = SDL_MapRGB(screen->format, 0x46, 0xd8, 0x16);
    	else if (vida <= 50 && vida > 25)
    		*p = SDL_MapRGB(screen->format, 0xf3, 0xaf, 0x16);
    	else
    		*p = SDL_MapRGB(screen->format, 0xfc, 0x14, 0x07);
    }
    else if(tipo.compare("magia")==0){
    	*p = SDL_MapRGB(screen->format, 0x10, 0x85, 0xc0);
    }

}


void BarraStatus::DrawBar(SDL_Surface* screen,string tipo, int param, int paramMax, int xpos, int ypos, int width, int hieght, SDL_Rect* box, SDL_Rect* bgbox, Uint32 color)
{

	box->x = xpos + 2;
    box->y = ypos + 2;
    box->w = width;
    box->h = hieght;

    bgbox->x = xpos ;
    bgbox->y = ypos ;
    bgbox->w = width + 4;
    bgbox->h = hieght + 4;

//    int xposmax = xpos + box->w;
    int yposmax = ypos + box->h;

    int porcentaje = floor(((float)param/paramMax*width));




    SDL_FillRect(screen, bgbox, color);
    for(int i = xpos+2 ; i< (xpos + porcentaje); i++)
    {
        for(int j= ypos + 2; j<= yposmax; j++)
        {
            putpixel(screen, tipo, i, j);
        }

    }
}



